//
//  TF_BackQuestionController.swift
//  TalkfunSDKDemo
//
//  Created by 邓盛明 on 2021/12/28.
//  Copyright © 2021 Talkfun. All rights reserved.
//

import UIKit


class TF_BackQuestionController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    var datasource:[TFPlaybackQuestionData] = []
    var nodataView:UIView!
    var tableview:UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    

    func createUI() {
        
        self.tableview = UITableView.init(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height))
        self.tableview.delegate = self
        self.tableview.dataSource = self
        self.tableview.backgroundColor = UIColor.clear
        self.tableview.register(TF_BackQuestionCell.self, forCellReuseIdentifier: "backQuestion")
        self.tableview.separatorStyle = UITableViewCell.SeparatorStyle.none
        self.view.addSubview(self.tableview)
        
        self.nodataView = UIView.init()
        self.nodataView.frame = self.view.bounds;
        self.view.addSubview(self.nodataView)
      
        let noImg = UIImageView.init()
        noImg.frame = CGRect(x: (self.nodataView.frame.size.width-200)/2.0, y: 60, width: 200, height: 150)
        noImg.image = UIImage.init(named: "HTLive.bundle/Default page_ask")
        self.nodataView.addSubview(noImg)
     
    
        let noLabel = UILabel.init()
        noLabel.text = "暂无提问";
        noLabel.font = UIFont.PingFang(size: 16)
        noLabel.textColor = UIColor(red: 20/255.0, green: 23/255.0, blue: 26/255.0, alpha: 1.0)
        noLabel.textAlignment = .center
        noLabel.frame = CGRect(x: 0, y: 176, width: self.nodataView.frame.size.width, height: 22)
        self.nodataView.addSubview(noLabel)

    }
  
    //回放提问信息
    func updatePlayBackQuestion(list:NSMutableArray?) {
        
         self.datasource.removeAll()
         list?.forEach({  model1 in
            self.datasource.append(model1 as! TFPlaybackQuestionData)
          })
          
          self.tableview.reloadData()
        let indexpath = NSIndexPath.init(row: self.datasource.count - 1, section: 0)
        self.tableview.scrollToRow(at: indexpath as IndexPath, at: .bottom, animated: true)
    
          if self.datasource.count > 0 {
              self.nodataView.isHidden = true
          } else {
              self.nodataView.isHidden = false
          }
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.datasource.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let model:TFPlaybackQuestionData = self.datasource[indexPath.row]
        
        return self.getHeight(model: model)
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "backQuestion", for: indexPath) as! TF_BackQuestionCell
     
        cell.cellwidth = self.view.frame.size.width
        cell.selectionStyle = .none
        cell.isQuestion = true
        cell.isChatArea = false
        cell.isOrientationLandscape = false
        let model:TFPlaybackQuestionData = self.datasource[indexPath.row]
        cell.setmodel(model: model)
        

        return cell
    }
    
    func getHeight(model:TFPlaybackQuestionData) -> CGFloat {

        let nickname = model.nickname + " "
        let str11 = nickname + model.content
        let AttributedString11 = NSMutableAttributedString.init(string: str11)
       
        let font:UIFont = UIFont.systemFont(ofSize: 14)
        
        let size = TalkfunSharedTools.getSizeLabel(labelString: AttributedString11, labelWidth:self.tableview.frame.size.width-35-16-12-16, labelfont: font)
        
        
    
        if (model.answer.count > 0) {
            var height:CGFloat = 0;
            var anserwidth:CGFloat = 0;
          
            for i in 0..<model.answer.count {
                let answer:TFPlaybackQuestionAnswer = model.answer[i] as! TFPlaybackQuestionAnswer
            
                let str1 = answer.nickname + answer.content
                let AttributedString1 = NSMutableAttributedString.init(string: str1)

                let attr132: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 14),]
                AttributedString1.addAttributes(attr132, range: NSRange(location: 0, length: str1.count))
             
                let attch = NSTextAttachment.init()
               
               
                attch.bounds = CGRect(x: 0, y: -3, width: 27, height: 15)
                let imageStr = NSAttributedString.init(attachment: attch)
                AttributedString1.insert(imageStr, at: answer.nickname.count+1)
                let Attri = NSAttributedString.init(string: " ")
                AttributedString1.insert(Attri, at: answer.nickname.count+2)
            
                
                let AttributeName: [NSAttributedString.Key : Any] = [.kern:5]
                AttributedString1.addAttributes(AttributeName, range: NSRange(location: answer.nickname.count+2, length: 1))
                
                
                let attr22: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 14),]
                
                AttributedString1.addAttributes(attr22, range: NSRange(location: answer.nickname.count+2, length: answer.content.count))
                let attr24: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 14),]
                AttributedString1.addAttributes(attr24, range: NSRange(location: 0, length: AttributedString1.length))
          
                let answersize = TalkfunSharedTools.getSizeLabel(labelString: AttributedString1, labelWidth:self.tableview.frame.size.width-35-16-12-16, labelfont: font)
  
                
                
               //取最大值
                anserwidth = answersize.width > anserwidth ? answersize.width: anserwidth
           
             
                height = height+answersize.height+10;
            
                
            }
            
            return size.height+10+1+height+8;

        } else {
            
            return size.height+8+12;
        
        }
        
    }
    
    
    

}
